# -*- coding: utf-8 -*-
# @Author: zishuiliufeng
# @Date:   2021-02-12 16:48:00
# @Last Modified by:   zishuiliufeng
# @Last Modified time: 2021-02-16 17:13:44
import sys
import os
import errno
import getInfo

def setFilePath():
    path = 'G:/RenpyGames/'
    return path
# 脚本输出角色定义
def character(game_id):
    path = setFilePath();
    file = path+"role.rpy"
    condition = [['game_id',game_id]]
    role_info = getInfo.get_info('role_info',condition)

    if role_info:
        for role in role_info:
            content = ''
            content = 'define %s = Character("%s")' %(role['en_name'], role['name'])
            getInfo.writeToTxt(file,content)

            roleAttribute(game_id)

            roleEvent(role['id'],role['en_name'])

            game(role['id'],role['en_name'],'')
            #print(content)
    else:
        print('角色为空')
#脚本输出角色属性
def roleAttribute(game_id):
    condition = [['game_id',game_id]]
    role_attribute = getInfo.get_info('role_attribute',condition)
    path = setFilePath();
    if role_attribute:
        for attr in role_attribute:
            role_id = attr['role_id']
            role_info = getInfo.get_info('role_info',[['id',role_id]])
            role_en_name = role_info[0]['en_name']

            file = path +role_en_name+"_attr.rpy"

            content = ''
            content = '$ '+attr['en_name'] + ' = 0'
            getInfo.writeToTxt(file,content)
            #print(content)

            
    else:
        print('角色属性为空')
#角色事件输出
def roleEvent(role_id,role_en_name):
    role_event = getInfo.get_info('role_event',[['role_id',role_id]])
    path = setFilePath();
    file = path + role_en_name + "_event.rpy"
    if role_event:
        for event in role_event:
            content = ''
            content = '#%s \n %s=false' %(name, en_name)

            getInfo.writeToTxt(file,content)
            #print(content)

        
#游戏内容输出
def game(role_id,role_en_name,event):
    game_info = getInfo.get_info('game_info',[['role_id',role_id],['status',0]])

    path = setFilePath();

    file = path + role_en_name + "_info.rpy"
    img_file  = path + role_en_name + "_img.rpy"
    movies_file  = path + role_en_name + "_movies.rpy"
    if game_info:
        for info in game_info:
            img_content = ''
            movie_content = ''
            game_content = ''

            img = info['img']
            movie = info['video']
            content = info['content']
            label_name = info['name']

            img_name = ''
            if img:
                img_arr = img.rsplit('/',1)
                img_arr_length = len(img_arr)
                img_name = img_arr[img_arr_length-1].split('.',1)[0]

                img_content = 'image %s = "%s"' %(img_name, img)
                getInfo.writeToTxt(img_file,img_content)
                #print(img_content)

            movie_name = ''
            if movie:
                movie_arr = movie.rsplit('/',1)
                movie_arr_length = len(movie_arr)
                movie_name = movie_arr[movie_arr_length-1].split('.',1)[0]

                movie_content = 'image  %s = Movie(channel="%s", play="%s")' %(movie_name, movie_name, movie)
                getInfo.writeToTxt(movies_file,movie_content)
                #print(movie_content)

            if content:
                #格式化内容
                content = formatContent(content)

                game_content = 'label %s:\n' %(label_name)
                #如果有触发条件
                trigger = ''
                if event:
                    trigger = event['trigger']
                if trigger:
                    game_content += trigger+'\n'

                if movie:
                    game_content += '    show %s with dissolve \n' %(movie_name)
                    game_content += content
                    game_content += '    $ renpy.pause() \n'
                    game_content += '    hide %s \n' %(movie_name)

                if img:
                    game_content += '    scene %s \n' %(img_name)
                    game_content += content
                    game_content += '    $ renpy.pause() \n'

                #如果有事件发生后的结果
                result = info['result']
                if result:
                    game_content += result

                game_content += '    jump [next_scene] \n'

                getInfo.writeToTxt(file,game_content)
                #print(game_content)

#格式化内容
def formatContent(content):
    #去除头尾空格
    content = content.strip()
    #判断content是否为空
    if len(content) == 0:
        return content

    #判断字符串中是否包含中文句号 "。",出现次数
    find1 = "。"
    num = content.count(find1)
    if num <= 1:
        return content

    new_content = ''
    #多次出现句号，表示多段语句，需要分行处理
    str_list = content.split('。')
    for row in str_list:
        #在renpy游戏中对话内容及旁白需要1个tab键4个space空格开头,遵循python格式
        #所以要先去除每一行开头可能存在的空格和换行符
        row = row.strip("\n")
        row = row.strip()
        #每一行的内容都需要判断是旁白还是对话
        row = isTalk(row)
        new_content += '    %s \n' %(row)
        new_content += '\n' #两段话中间空一行最好

    return new_content

#判断是对话还是旁白
def isTalk(content):
    #在renpy游戏中对话是需要在对话前加上说话人的 英文简称+空格+对话内容,如 w "这里一个人都没有..."
    content = content.strip()
    if len(content) == 0:
        return ''

    #判断是否包含空格,按照空格拆分，再判断list中第一个是否全是英文
    find2 = ' '

    if content.find(find2) == -1:
        #不是对话，是旁白，需要处理
        #旁白末尾需要加上句号"。"
        #单行旁白需要在首尾加上英文引号""
        #多行可以用"""xxx"""三引号包裹
        #为了简单处理，就全采用单引号，不用判断行数
        content = content.strip("\n")
        content = content.strip('"')
        content = '"%s"' %(content)
        return content

    talk_list = content.split(' ',1)
    num = len(talk_list)

    if num > 1:
        en_name = talk_list[0]
           
        talk_content = talk_list[1]
        #判断是否全为字母,返回 True 或 False
        is_en = en_name.isalpha()
        if is_en:
            #是对话,判断对话内容中是否包含中文的引号 “”，如果有，需要替换为英文引号""
            #如果没有，需要加上
            
            find3 = '“'
            find4 = '”'
            if talk_content.find(find3) > -1: #find方法，如果包含子字符串返回开始的索引值，否则返回-1。
                talk_content = talk_content.replace(find3,'"')
            else:
                talk_content = '"'+talk_content

            if talk_content.find(find4) > -1: #find方法，如果包含子字符串返回开始的索引值，否则返回-1。
                talk_content = talk_content.replace(find4,'"')
            else:
                talk_content += '"'

            content = en_name+' '+talk_content

            return content
    else:
        #不是对话，是旁白，需要处理
        #旁白末尾需要加上句号"。"
        #单行旁白需要在首尾加上英文引号""
        #多行可以用"""xxx"""三引号包裹
        #为了简单处理，就全采用单引号，不用判断行数
        content = content.strip("\n")
        content = content.strip('"')
        content = '"%s。"' %(content)

        return content

        